﻿/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.    
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.    THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*
 * Portions of this software Copyright (c) 1995 Brown University.
 * All rights reserved.
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement
 * is hereby granted, provided that the above copyright notice and the
 * following two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE.    THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*
Portions copyright (c) 2012 Eric Lasota

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* Correct bad motion information */
#define LOOSE_MPEG

using System;
using System.Collections.Generic;
using System.Text;

namespace MPEGnet
{
    internal class video
    {
        public static byte BClamp(int coef)
        {
            if (coef <= 0)
                return 0;
            if (coef > 255)
                return 255;
            return (byte)coef;
        }

        /* Set up array for fast conversion from zig zag order to row/column
             coordinates.
        */
        public static int[] ZIGZAG =
        {
                0 + 0*8, 1 + 0*8, 0 + 1*8, 0 + 2*8, 1 + 1*8,
                2 + 0*8, 3 + 0*8, 2 + 1*8, 1 + 2*8, 0 + 3*8,
                0 + 4*8, 1 + 3*8, 2 + 2*8, 3 + 1*8, 4 + 0*8,
                5 + 0*8, 4 + 1*8, 3 + 2*8, 2 + 3*8, 1 + 4*8,
                0 + 5*8, 0 + 6*8, 1 + 5*8, 2 + 4*8, 3 + 3*8,
                4 + 2*8, 5 + 1*8, 6 + 0*8, 7 + 0*8, 6 + 1*8,
                5 + 2*8, 4 + 3*8, 3 + 4*8, 2 + 5*8, 1 + 6*8,
                0 + 7*8, 1 + 7*8, 2 + 6*8, 3 + 5*8, 4 + 4*8,
                5 + 3*8, 6 + 2*8, 7 + 1*8, 7 + 2*8, 6 + 3*8,
                5 + 4*8, 4 + 5*8, 3 + 6*8, 2 + 7*8, 3 + 7*8,
                4 + 6*8, 5 + 5*8, 6 + 4*8, 7 + 3*8, 7 + 4*8,
                6 + 5*8, 5 + 6*8, 4 + 7*8, 5 + 7*8, 6 + 6*8,
                7 + 5*8, 7 + 6*8, 6 + 7*8, 7 + 7*8
        };

        static byte crop(short x)
        {
            if(x<=0)
                return 0;
            if(x>=255)
                return 255;
            return (byte)x;
        }
    }
}

